<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="icon" href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/lib/static/common/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/lib/static/common/js/jquery.form.js"></script>
    <script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layui-fluid">
    <hr class="layui-bg-red">
    <table class="layui-hide" id="roleTable" lay-filter="RoleMonitor"></table>
    <input type="hidden" id="id" name="id">
    <div class="layui-btn-block layui-col-sm6 layui-col-sm-offset5">
        <button id="save" class="layui-btn" data-type="getCheckData">保存</button>
        <button id="reset" type="reset" class="layui-btn layui-btn-primary"
                onclick="window.location.href='${pageContext.request.contextPath}/sys/role/role_accredit'">重置</button>
    </div>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs " lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="accredit">授权</a>
        <%--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" lay-filter="del" >删除</a>--%>

    </script>
    <script>
        var table = null;
        var baseUrl = '${pageContext.request.contextPath}/sys/role/';
        layui.use(['jquery','table', 'form'], function () {
            table = layui.table;
            var form = layui.form;
            layer = layui.layer;
            table.render({
                elem: '#roleTable'
                ,title: "角色数据"
                ,defaultToolbar:['filter']
                , url: baseUrl +'get'
                , request: {
                    pageName: 'pageNo' //页码的参数名称，默认：page
                    , limitName: 'pageSize' //每页数据量的参数名，默认：limit
                }
                ,height: 'full-70'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , cols: [[{type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', sort: true}
                    , {field: 'roleName', title: '<i class="layui-icon">&#xe612;</i> 角色名称'} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    , {field: 'roleDesc', title: '<i class="layui-icon">&#xe60b;</i> 角色描述'}
                ]]
                , page: {
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
                    , groups: 5
                }
            });
        });
        $(function () {
            $(".layui-btn-block")
                .on("click","#save",accredit)
           /* layer.iframeAuto(index);*/
        })
        function accredit() {
            var checkStatus = table.checkStatus('roleTable')
            data = checkStatus.data;
            var ids = [];
            $.each( data, function (index, value) {
                var id = value.id;
                ids.push(id);
            });
            var params = {
                userId:$("#id").val(),
                roleId:ids.toString(),
                isdel:0
            }
            var url = '${pageContext.request.contextPath}/sys/user/user_role_add'
            if(data.length==0){
                layer.alert('必须为用户分配一个角色资源', {
                    skin: 'layui-layer-molv'
                    , closeBtn: 0
                });
            }else {
                $.post(
                    url,
                    params,
                    function (result) {
                        if (result.code == 0) {
                            layer.msg(result.msg, {icon: 1, time: 500}, function () {
                                parent.window.location.reload();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 5, anim: 6});
                        }
                    },
                    "json"
                );
            }
        }


    </script>
</div>

</div>
</body>
</html>
